package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.entity.bo.DataDictionaryBo;
import com.mf.entity.request.dataDictionary.DataDictionaryRequest;
import com.mf.entity.vo.DataDictionaryVo;
import com.mf.enums.EnableOrDisableEnum;
import com.mf.exceptions.GraceException;
import com.mf.mapper.DataDictionaryMapper;

import com.mf.service.IDataDictionaryService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.utils.AuthContextUtil;
import com.mf.utils.MybatisPlusUtils;
import com.mf.utils.VerifyUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
*  服务实现类
*
* @author mf
* @since 2024-07-19
*/
@Service
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionaryBo> implements IDataDictionaryService {
    @Resource
    private DataDictionaryMapper dataDictionaryMapper;

    @Override
    public Integer add(DataDictionaryRequest dataDictionaryRequest) {
        if(dataDictionaryRequest.getPid() != 0){
            DataDictionaryVo dictionaryVo = findById(dataDictionaryRequest.getPid());
            if(dictionaryVo == null){
                GraceException.display("父级字典不存在");
            }
        }


        List<DataDictionaryBo> dataDictionaryBos = new ArrayList<>();
        String[] values = dataDictionaryRequest.getDctValue().split(";");
        for (String value : values) {
            DataDictionaryBo dataDictionaryBo = new DataDictionaryBo();
            BeanUtils.copyProperties(dataDictionaryRequest, dataDictionaryBo);
            dataDictionaryBo.setDctValue(value);
            dataDictionaryBos.add(dataDictionaryBo);
        }

        return dataDictionaryMapper.insertBatch(dataDictionaryBos);
    }

    /**
     * 编辑字典
     */
    @Override
    public Integer edit(DataDictionaryRequest dataDictionaryRequest) {
        dataDictionaryRequest.setCode(null);
        DataDictionaryBo dictionaryBo = new DataDictionaryBo();
        BeanUtils.copyProperties(dataDictionaryRequest, dictionaryBo);
        return dataDictionaryMapper.updateById(dictionaryBo);
    }

    /**
     * 根据编号查询
     */
    @Override
    public List<DataDictionaryVo> findByCode(String code) {
        LambdaQueryWrapper<DataDictionaryBo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionaryBo::getCode, code)
                .eq(DataDictionaryBo::getStatus, EnableOrDisableEnum.Enable.value)
                .eq(DataDictionaryBo::getSystemCode, AuthContextUtil.getUser().getSystemCode())
                .orderByDesc(DataDictionaryBo::getSort);

        return MybatisPlusUtils.changeDataClass(dataDictionaryMapper.selectList(queryWrapper), DataDictionaryVo.class);
    }

    /**
     * 根据id查询字典
     */
    @Override
    public DataDictionaryVo findById(Integer id) {
        DataDictionaryBo dictionaryBo = dataDictionaryMapper.selectById(id);
        if(VerifyUtils.isNotEmpty(dictionaryBo)){
            DataDictionaryVo dataDictionaryVo = new DataDictionaryVo();
            BeanUtils.copyProperties(dictionaryBo, dataDictionaryVo);
            return dataDictionaryVo;
        }
        return null;
    }
}
